package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class ClientPractice {
    private Socket socket;

    public ClientPractice() {
        try {
            System.out.println("连接服务端");
            socket = new Socket("localhost", 8088);
            System.out.println("服务端连接完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        try {
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw, true);
            Scanner scan = new Scanner(System.in);
            String line;
            while (!("exit".equalsIgnoreCase(line = scan.nextLine()))) {
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    public static void main(String[] args) {
        ClientPractice c = new ClientPractice();
        c.start();
    }
}
